package com.jinly.ryder.manage.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.common.auth.annotation.Auth;
import com.jinly.ryder.common.auth.annotation.AuthParam;
import com.jinly.ryder.common.auth.model.AuthRole;
import com.jinly.ryder.common.auth.model.AuthType;
import com.jinly.ryder.manage.service.AppInstanceService;
import com.jinly.ryder.manage.web.mapping.AppInstanceVOMapping;
import com.jinly.ryder.manage.web.vo.AppInstanceVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("manage/appInstance")
@RequiredArgsConstructor
public class AppInstanceController {

    private final AppInstanceVOMapping appInstanceVOMapping;
    private final AppInstanceService appInstanceService;

    @GetMapping("list")
    @Auth(type = AuthType.APP, role = AuthRole.DEVELOPER, value = "#{appName}")
    public List<AppInstanceVO> groupList(@AuthParam("appName") String appName, String deployGroupName) {
        return appInstanceVOMapping.toVoList(appInstanceService.list(appName, deployGroupName));
    }

    @GetMapping("page")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "app")
    public Page<AppInstanceVO> instancePage(Long current, Long size, AppInstanceVO query) {
        return appInstanceVOMapping.toVOPage(appInstanceService.instancePage(new Page(current, size), appInstanceVOMapping.toModel(query)));
    }

    @PostMapping()
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "app")
    public void save(@RequestBody AppInstanceVO appInstanceVO) {
        appInstanceService.save(appInstanceVOMapping.toModel(appInstanceVO));
    }
}
